package com.example.mvvm.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.example.mvvm.R;
import com.example.mvvm.mode.LookVideoMode;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
import com.shuyu.gsyvideoplayer.video.NormalGSYVideoPlayer;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoControlView;

import java.util.Random;

/**
 * 视频播放
 */
public class MyVideoPlayer extends NormalGSYVideoPlayer {

    float[] num = {-30, -20, 0, 20, 30};//随机心形图片角度

    public MyVideoPlayer(Context context, Boolean fullFlag) {
        super(context, fullFlag);
        hideAllWidget();
    }
    public void setMode(LookVideoMode mode, int position) {
        getThumbImageViewLayout().setVisibility(VISIBLE);
        ImageView view = new ImageView(mContext);
        Glide.with(mContext).load(mode.getMp4_info().getImg()).into(view);
        getBackButton().setVisibility(GONE);
        new GSYVideoOptionBuilder()
                .setIsTouchWiget(false)
                .setThumbImageView(view)
                .setUrl(mode.getMp4_info().getPath())
                .setVideoTitle("")
                .setCacheWithPlay(true)
                .setRotateViewAuto(false)
                .setLockLand(true)
                .setPlayTag("ProgressVideoPlayer" + position)
                .setShowFullAnimation(true)
                .setNeedLockFull(true)
                .setPlayPosition(position)
                .build(this);
    }



    public MyVideoPlayer(Context context) {
        super(context);
        hideAllWidget();
    }

    public MyVideoPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
        hideAllWidget();
    }


    @Override
    public int getLayoutId() {
        return R.layout.my_video_normal;
    }


    @Override
    protected void onClickUiToggle(MotionEvent e) {
    }

    public boolean onTouch(View v, MotionEvent event) {
        gesture.onTouchEvent(event);
        return false;
    }

    /**
     * 双击
     */
    protected GestureDetector gesture = new GestureDetector(getContext().getApplicationContext(), new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTap(MotionEvent event) {
            ImageView imageView = new ImageView(mContext);
            //设置展示的位置，需要在手指触摸的位置上方，即触摸点是心形的右下角的位置
            LayoutParams params = new LayoutParams(300, 300);
            params.leftMargin = (int) event.getX() - 150;
            params.topMargin = (int) event.getY() - 300;
            //设置图片资源

            Drawable drawable = ContextCompat.getDrawable(mContext, R.mipmap.sp_dianzanhou_da);
            imageView.setImageDrawable(drawable);
            imageView.setLayoutParams(params);
            //把IV添加到父布局当中
            addView(imageView);
            //设置控件的动画
            AnimatorSet animatorSet = new AnimatorSet();
            //缩放动画，X轴2倍缩小至0.9倍
            animatorSet.play(scale(imageView, "scaleX", 2f, 0.9f, 100, 0))
                    //缩放动画，Y轴2倍缩放至0.9倍
                    .with(scale(imageView, "scaleY", 2f, 0.9f, 100, 0))
                    //旋转动画，随机旋转角
                    .with(rotation(imageView, 0, 0, num[new Random().nextInt(4)]))
                    //渐变透明动画，透明度从0-1
                    .with(alpha(imageView, 0, 1, 100, 0))
                    //缩放动画，X轴0.9倍缩小至
                    .with(scale(imageView, "scaleX", 0.9f, 1, 50, 150))
                    //缩放动画，Y轴0.9倍缩放至
                    .with(scale(imageView, "scaleY", 0.9f, 1, 50, 150))
                    //位移动画，Y轴从0上移至600
                    .with(translationY(imageView, 0, -600, 800, 400))
                    //透明动画，从1-0
                    .with(alpha(imageView, 1, 0, 300, 400))
                    //缩放动画，X轴1至3倍
                    .with(scale(imageView, "scaleX", 1, 3f, 700, 400))
                    //缩放动画，Y轴1至3倍
                    .with(scale(imageView, "scaleY", 1, 3f, 700, 400));
            //开始动画
            animatorSet.start();
            //设置动画结束监听
            animatorSet.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    //当动画结束以后，需要把控件从父布局移除
                    removeViewInLayout(imageView);
                }
            });
            return super.onDoubleTap(event);

        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            touchDoubleUp(e);
            return super.onSingleTapConfirmed(e);
        }

        @Override
        public void onLongPress(MotionEvent e) {
            super.onLongPress(e);
            touchLongPress(e);
        }
    });


    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
    }


    /**
     * 缩放动画
     * @param view
     * @param propertyName
     * @param from
     * @param to
     * @param time
     * @param delayTime
     * @return
     */
    public static ObjectAnimator scale(View view, String propertyName, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , propertyName
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }
    /**
     * 位移动画
     * @param view
     * @param from
     * @param to
     * @param time
     * @param delayTime
     * @return
     */
    public static ObjectAnimator translationX(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationX"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }
    public static ObjectAnimator translationY(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationY"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }
    /**
     * 透明度动画
     * @param view
     * @param from
     * @param to
     * @param time
     * @param delayTime
     * @return
     */
    public static ObjectAnimator alpha(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "alpha"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }
    public static ObjectAnimator rotation(View view, long time, long delayTime, float... values) {
        ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", values);
        rotation.setDuration(time);
        rotation.setStartDelay(delayTime);
        rotation.setInterpolator(input -> input);
        return rotation;
    }

}
